<?php
class pdf_invoice {
  function Header()
  {

  }
  function Footer()
  {

  }
  function fetch_data($docdata) {
    global $masterclass;

    $this->companylineone=$docdata['invoiceinfo']->fields['companylineone'];
    $this->companylinetwo=$docdata['invoiceinfo']->fields['companylinetwo'];
    $this->companylinethree=$docdata['invoiceinfo']->fields['companylinethree'];
    $this->companylinefour=$docdata['invoiceinfo']->fields['companylinefour'];
    $this->companylinefive=$docdata['invoiceinfo']->fields['companylinefive'];

    $this->recipientlineone=$docdata['invoiceinfo']->fields['recipientlineone'];
    $this->recipientlinetwo=$docdata['invoiceinfo']->fields['recipientlinetwo'];
    $this->recipientlinethree=$docdata['invoiceinfo']->fields['recipientlinethree'];
    $this->recipientlinefour=$docdata['invoiceinfo']->fields['recipientlinefour'];
    $this->recipientlinefive=$docdata['invoiceinfo']->fields['recipientlinefive'];

    if ($docdata['invoiceinfo']->fields['documentdate']!='' && $docdata['invoiceinfo']->fields['documentdate']!='0000-00-00') { $this->documentdate = date("l, M dS Y",strtotime($docdata['invoiceinfo']->fields['documentdate'])); } else { $this->documentdate=''; }
    if ($docdata['invoiceinfo']->fields['duedate']!='' && $docdata['invoiceinfo']->fields['duedate']!='0000-00-00') { $this->documentduedate = date("l, M dS Y",strtotime($docdata['invoiceinfo']->fields['duedate'])); } else { $this->documentduedate=''; }

    $this->documentid=$docdata['invoiceinfo']->fields['documentid'];
    $this->documenttype=$docdata['invoiceinfo']->fields['documenttype'];
    $this->net=$docdata['invoiceinfo']->fields['net'];
    switch ($this->net) {
     case '0':
      $this->net='Upon Request';
     break;
     default:
      $this->net='Net '.$this->net;
     break;
    }
    $this->invoice_items=array();

    if (!empty($docdata['invoiceitems'])) {

      $arrinvoicetotals=$masterclass->allclasses['invoices']->calc_invoicetotal($docdata['invoiceitems']);

      foreach ($docdata['invoiceitems'] as $invoiceitem) {
        $tmparray=array();
        $tmparray['qty']=$invoiceitem['qty'];
        $tmparray['name']=$invoiceitem['name'];
        $tmparray['description']=$invoiceitem['description'];
        $tmparray['cost']=$invoiceitem['cost'];
        $tmparray['taxed']=$invoiceitem['taxed'];
        $tmparray['taxpercent']=$invoiceitem['taxpercent'];
        $tmparray['updated']=$invoiceitem['updated'];
        $tmparray['datestamp']=$invoiceitem['datestamp'];

        if (!preg_match('/^[0-9.-]+$/iD', $tmparray['qty'])) { $tmparray['qty']=1; }
        if (!preg_match('/^[0-9.-]+$/iD', $tmparray['cost'])) { $tmparray['cost']=0; }
        if (!preg_match('/^[0-9.-]+$/iD', $tmparray['taxpercent'])) { $tmparray['taxpercent']=0; }

        $this->doqty=1;
        if ($tmparray['taxed']=='1') { $this->dotax=1; }

        if ($tmparray['cost']<0) { $polarity='-'; } else { $polarity='+'; }

        $tmparray['subtotal']=$tmparray['qty']*abs($tmparray['cost']);
        $tmparray['taxcost']=$tmparray['subtotal']*($tmparray['taxpercent']/100);
        $tmparray['total']=$tmparray['subtotal']+$tmparray['taxcost'];

        if (preg_match('/^[0-9.-]+$/iD', $tmparray['cost'])) { $tmparray['cost']=number_format($tmparray['cost'],2); }
        if (preg_match('/^[0-9.-]+$/iD', $tmparray['subtotal'])) { $tmparray['subtotal']=number_format($tmparray['subtotal'],2); }
        if (preg_match('/^[0-9.-]+$/iD', $tmparray['taxcost'])) { $tmparray['taxcost']=number_format($tmparray['taxcost'],2); }
        if (preg_match('/^[0-9.-]+$/iD', $tmparray['total'])) { $tmparray['total']=number_format($tmparray['total'],2); }

        $this->invoice_items[]=$tmparray;
      }
    }

    if (preg_match('/^[0-9.-]+$/iD', $arrinvoicetotals['qty'])) {$this->totalqty=number_format($arrinvoicetotals['qty'],2); } else { $this->totalqty=$arrinvoicetotals['qty']; }
    if (preg_match('/^[0-9.-]+$/iD', $arrinvoicetotals['tax'])) {$this->totaltax=number_format($arrinvoicetotals['tax'],2); } else { $this->totaltax=$arrinvoicetotals['tax']; }
    if (preg_match('/^[0-9.-]+$/iD', $arrinvoicetotals['cost'])) {$this->totalcost=number_format($arrinvoicetotals['cost'],2); } else { $this->totalcost=$arrinvoicetotals['cost']; }
    if (preg_match('/^[0-9.-]+$/iD', $arrinvoicetotals['subtotal'])) {$this->subtotal=number_format($arrinvoicetotals['subtotal'],2); } else { $this->subtotal=$arrinvoicetotals['subtotal']; }
    if (preg_match('/^[0-9.-]+$/iD', $arrinvoicetotals['total'])) {$this->total=number_format($arrinvoicetotals['total'],2); } else { $this->arrinvoicetotals['total']; }

    return;
  }


  function page_layout() {
    global $masterclass;

    $masterclass->allclasses['paperpdf']->AddPage('P','mm','lettersm');
    $masterclass->allclasses['paperpdf']->SetMargins(0,0,0);
    $masterclass->allclasses['paperpdf']->SetAutoPageBreak(0,0);
    $masterclass->allclasses['paperpdf']->SetFont('Arial','','10');

    $masterclass->allclasses['paperpdf']->SetFillColor(221,221,221);

    $masterclass->allclasses['paperpdf']->SetFont('Arial','I',10);
    $masterclass->allclasses['paperpdf']->SetXY(115,10);
    $masterclass->allclasses['paperpdf']->Cell(60,3,'Date:',0,0,'L');
    $masterclass->allclasses['paperpdf']->SetXY(175,10);
    $masterclass->allclasses['paperpdf']->Cell(20,3,' #:',0,0,'R');
    $masterclass->allclasses['paperpdf']->SetFont('Arial','',9);
    $masterclass->allclasses['paperpdf']->SetXY(115,13);
    $masterclass->allclasses['paperpdf']->Cell(60,4,'',1,0,'L',1);
    $masterclass->allclasses['paperpdf']->SetXY(175,13);
    $masterclass->allclasses['paperpdf']->Cell(20,4,'',1,0,'R',1);
    $masterclass->allclasses['paperpdf']->SetFont('Arial','I',10);
    $masterclass->allclasses['paperpdf']->SetXY(115,18);

	$masterclass->allclasses['paperpdf']->SetFont('Arial','B',9);
	$masterclass->allclasses['paperpdf']->SetXY(17,46);
	$masterclass->allclasses['paperpdf']->Cell(44,3,'Status:',0,0,'C');
	$masterclass->allclasses['paperpdf']->SetXY(59,46);
	$masterclass->allclasses['paperpdf']->Cell(45,3,'Terms:',0,0,'C');
	$masterclass->allclasses['paperpdf']->SetXY(104,46);
	$masterclass->allclasses['paperpdf']->Cell(45,3,'Due Date:',0,0,'C');
	$masterclass->allclasses['paperpdf']->SetXY(149,46);
	$masterclass->allclasses['paperpdf']->Cell(44,3,'P.O. #:',0,0,'C');

	$masterclass->allclasses['paperpdf']->SetFont('Arial','',7);
	$masterclass->allclasses['paperpdf']->SetXY(17,50);
	$masterclass->allclasses['paperpdf']->Cell(44,3,'',1,0,'C',1);
	$masterclass->allclasses['paperpdf']->SetXY(59,50);
	$masterclass->allclasses['paperpdf']->Cell(45,3,'',1,0,'C',1);
	$masterclass->allclasses['paperpdf']->SetXY(104,50);
	$masterclass->allclasses['paperpdf']->Cell(45,3,'',1,0,'C',1);
	$masterclass->allclasses['paperpdf']->SetXY(149,50);
	$masterclass->allclasses['paperpdf']->Cell(44,3,'',1,0,'C',1);


	$masterclass->allclasses['paperpdf']->Line(0,$masterclass->allclasses['paperpdf']->GetY()+5,$masterclass->allclasses['paperpdf']->GetX()+170,$masterclass->allclasses['paperpdf']->GetY()+5);
	$masterclass->allclasses['paperpdf']->Ln(7);

    $itemw=124;
    $costw=28;
    $qtyw=0;
    if ($this->doqty==1) { $itemw-=10;$qtyw=10; }
    if ($this->dotax==1) { $itemw-=40; }

    if ($this->dotax==1) { $costw-=8; }

	$masterclass->allclasses['paperpdf']->SetFont('Arial','B',9);
	$masterclass->allclasses['paperpdf']->SetX(15);
	if ($this->doqty==1) { $masterclass->allclasses['paperpdf']->Cell($qtyw,5,'Qty',1,0,'C',1); }
	$masterclass->allclasses['paperpdf']->Cell($itemw,5,'Item',1,0,'L',1);
	$masterclass->allclasses['paperpdf']->Cell($costw,5,'Cost Per',1,0,'C',1);
	if ($this->dotax==1) {
	  $masterclass->allclasses['paperpdf']->Cell(28,5,'Subtotal',1,0,'C',1);
	  $masterclass->allclasses['paperpdf']->Cell(20,5,'Tax',1,0,'C',1);
	}
	$masterclass->allclasses['paperpdf']->Cell(28,5,'Total',1,1,'C',1);

    $masterclass->allclasses['paperpdf']->Line(15,$masterclass->allclasses['paperpdf']->GetY(),15,248);
    if ($this->doqty==1) { $masterclass->allclasses['paperpdf']->Line(25,$masterclass->allclasses['paperpdf']->GetY(),25,248); }
    $masterclass->allclasses['paperpdf']->Line($qtyw+$itemw+15,$masterclass->allclasses['paperpdf']->GetY(),$qtyw+$itemw+15,248);
    if ($this->dotax==1) {
      $masterclass->allclasses['paperpdf']->Line(119,$masterclass->allclasses['paperpdf']->GetY(),119,248);
      $masterclass->allclasses['paperpdf']->Line(147,$masterclass->allclasses['paperpdf']->GetY(),147,248);
    }
    $masterclass->allclasses['paperpdf']->Line(167,$masterclass->allclasses['paperpdf']->GetY(),167,248);
    $masterclass->allclasses['paperpdf']->Line(195,$masterclass->allclasses['paperpdf']->GetY(),195,248);

    $masterclass->allclasses['paperpdf']->SetXY(15,248);
    $masterclass->allclasses['paperpdf']->SetFont('Arial','B',10);
    $masterclass->allclasses['paperpdf']->MultiCell(146,5,'Subtotal: $ '.chr(13).chr(10).''.' Sales Tax '.''.': $ '.chr(13).chr(10).'Grand Total: $ ',1,'R',1);
    $masterclass->allclasses['paperpdf']->SetXY(161,248);
    $masterclass->allclasses['paperpdf']->MultiCell(34,5,' '.chr(13).chr(10).' '.chr(13).chr(10).' ','TRB','R',1);


    $masterclass->allclasses['paperpdf']->SetY(264);
    $masterclass->allclasses['paperpdf']->SetFont('Arial','I',8);
    $masterclass->allclasses['paperpdf']->Cell(0,10,'Page '.$masterclass->allclasses['paperpdf']->PageNo().'/{nb}',0,0,'C');


  }


  function apply_data() {
    global $masterclass;
    // array(String, BottomLeft x, BottomLeft  y, Left x, Top y, Rows, align (L|C|R), toppadding, leftpadding, bottompadding, rightpadding

    $this->pagey=10;
    $this->company_info();
    $this->receipt_info();
    $this->document_totals();

    $itemcount=0;
    if (isset($this->invoice_items) && !empty($this->invoice_items)) {
      $this->pagey=60;
      foreach ($this->invoice_items as $item) {

        $masterclass->allclasses['paperpdf']->SetFont('Arial','',8);
        $this->pagey=$this->pagey+2.9;

        if (!isset($this->bgfillcolor) || $this->bgfillcolor=='ltgrey') {
          $this->bgfillcolor='white';
        } else {
          $this->bgfillcolor='ltgrey';
        }

        $newpage=0;
        $linemax=0;

        $txt1 = $item['qty'];
        $txt2 = $item['name'];
        if ($item['description']!='') { $txt2 .= "\n".$item['description']; }
        $txt3 = '$'.$item['cost'];
        $txt4 = '$'.$item['subtotal'];
        if ($item['taxpercent']>0) { $txt5=' '.$item['taxpercent'].'% $'.$item['taxcost']; }  else { $txt5=''; }
        $txt6 = '$'.$item['total'];

        $itemw=124;
        $costw=28;
        $qtyw=0;
        if ($this->doqty==1) { $itemw-=10;$qtyw=10; }
        if ($this->dotax==1) { $itemw-=40; }

        if ($this->dotax==1) { $costw-=8; }

        if ($this->doqty==1) { $txt1lines=$masterclass->allclasses['paperpdf']->getLines($txt1,10); if ($txt1lines==0) { $txt1lines=1; } }
        $txt2lines=$masterclass->allclasses['paperpdf']->getLines($txt2,$itemw); if ($txt2lines==0) { $txt2lines=1; }
        $txt3lines=$masterclass->allclasses['paperpdf']->getLines($txt3,$costw); if ($txt3lines==0) { $txt3lines=1; }
        if ($this->dotax==1) {
          $txt4lines=$masterclass->allclasses['paperpdf']->getLines($txt4,28); if ($txt4lines==0) { $txt4lines=1; }
          $txt5lines=$masterclass->allclasses['paperpdf']->getLines($txt5,20); if ($txt5lines==0) { $txt5lines=1; }
        }
        $txt6lines=$masterclass->allclasses['paperpdf']->getLines($txt6,28); if ($txt6lines==0) { $txt6lines=1; }

        for ($a=1;$a<6;$a++) {
          if (($this->pagey+(${'txt'.$a.'lines'}*3))>248) { $newpage=1; }
          if (${'txt'.$a.'lines'}>$linemax) { $linemax=${'txt'.$a.'lines'}; }
        }
        for ($a=1;$a<6;$a++) {
           for ($b=${'txt'.$a.'lines'};$b<=$linemax;$b++) { ${'txt'.$a} .= "\n"; }
        }
        if ($newpage==1 && $itemcount>0) {
          $this->page_layout();
          $this->company_info();
          $this->receipt_info();
          $this->document_totals();
          $this->pagey=67;
          $masterclass->allclasses['paperpdf']->SetFont('Arial','',8);
        }


        $masterclass->allclasses['paperpdf']->SetXY(15,$this->pagey);
        if ($this->doqty==1) {
          $txt1=str_replace('<br>',Chr(13).Chr(10),$txt1);
          $masterclass->allclasses['paperpdf']->MultiCell($qtyw,3,$txt1,0,'C',0);
          $curY1=$masterclass->allclasses['paperpdf']->GetY();
          $masterclass->allclasses['paperpdf']->SetXY(25,$this->pagey);
        }

        $txt2=str_replace('<br>',Chr(13).Chr(10),$txt2);
        $masterclass->allclasses['paperpdf']->MultiCell($itemw,3,$txt2,0,'L',0);
        $curY2=$masterclass->allclasses['paperpdf']->GetY();

        $masterclass->allclasses['paperpdf']->SetXY($qtyw+$itemw+15,$this->pagey);
        $txt3=str_replace('<br>',Chr(13).Chr(10),$txt3);
        $masterclass->allclasses['paperpdf']->MultiCell($costw,3,$txt3,0,'R',0);
        $curY3=$masterclass->allclasses['paperpdf']->GetY();

        if ($this->dotax==1) {
          $masterclass->allclasses['paperpdf']->SetXY(119,$this->pagey);
          $txt4=str_replace('<br>',Chr(13).Chr(10),$txt4);
          $masterclass->allclasses['paperpdf']->MultiCell(28,3,$txt4,0,'R',0);
          $curY4=$masterclass->allclasses['paperpdf']->GetY();

          $masterclass->allclasses['paperpdf']->SetXY(147,$this->pagey);
          $txt5=str_replace('<br>',Chr(13).Chr(10),$txt5);
          $masterclass->allclasses['paperpdf']->MultiCell(20,3,$txt5,0,'R',0);
          $curY5=$masterclass->allclasses['paperpdf']->GetY();
        }

        $masterclass->allclasses['paperpdf']->SetXY(167,$this->pagey);
        $txt6=str_replace('<br>',Chr(13).Chr(10),$txt6);
        $masterclass->allclasses['paperpdf']->MultiCell(28,3,$txt6,0,'R',0);
        $curY6=$masterclass->allclasses['paperpdf']->GetY();

        for ($a=1;$a<6;$a++) {
           if (${'curY'.$a}>$this->pagey) { $this->pagey=${'curY'.$a}; }
        }
        //$masterclass->allclasses['paperpdf']->Line(15,$this->pagey,195,$this->pagey);

        $itemcount+=1;
      }
    }

  }


  function company_info() {
   global $masterclass;

    if (isset($this->companylineone) && $this->companylineone!='') {
      $masterclass->allclasses['paperpdf']->SetFont('Arial','',10);
      $masterclass->allclasses['paperpdf']->SetXY(17,$this->pagey);
      $masterclass->allclasses['paperpdf']->Cell(35,6,$this->companylineone,0,0,'L');
      $this->pagey += 4;
    }
    if (isset($this->companylinetwo) && $this->companylinetwo!='') {
      $masterclass->allclasses['paperpdf']->SetFont('Arial','',10);
      $masterclass->allclasses['paperpdf']->SetXY(17,$this->pagey);
      $masterclass->allclasses['paperpdf']->Cell(35,6,$this->companylinetwo,0,0,'L');
      $this->pagey += 4;
    }
    if (isset($this->companylinethree) && $this->companylinethree!='') {
      $masterclass->allclasses['paperpdf']->SetFont('Arial','',10);
      $masterclass->allclasses['paperpdf']->SetXY(17,$this->pagey);
      $masterclass->allclasses['paperpdf']->Cell(35,6,$this->companylinethree,0,0,'L');
      $this->pagey += 4;
    }
    if (isset($this->companylinefour) && $this->companylinefour!='') {
      $masterclass->allclasses['paperpdf']->SetFont('Arial','',10);
      $masterclass->allclasses['paperpdf']->SetXY(17,$this->pagey);
      $masterclass->allclasses['paperpdf']->Cell(35,6,$this->companylinefour,0,0,'L');
      $this->pagey += 4;
    }
    if (isset($this->companylinefive) && $this->companylinefive!='') {
      $masterclass->allclasses['paperpdf']->SetFont('Arial','',10);
      $masterclass->allclasses['paperpdf']->SetXY(17,$this->pagey);
      $masterclass->allclasses['paperpdf']->Cell(35,6,$this->companylinefive,0,0,'L');
      $this->pagey += 4;
    }

  }
  function receipt_info() {
    global $masterclass;
    $this->pagey=22;
    $masterclass->allclasses['paperpdf']->SetFont('Arial','I',10);
    $masterclass->allclasses['paperpdf']->Text(115,$this->pagey,'Bill To: ');
    $this->pagey += 2;

    $recipientinfo = '';
    if (isset($this->recipientlineone) &&$this->recipientlineone!='') {
      $recipientinfo .= $this->recipientlineone."\n";
    }
     if (isset($this->recipientlinetwo) & $this->recipientlinetwo!='') {
      $recipientinfo .= $this->recipientlinetwo."\n";
    }
    if (isset($this->recipientlinethree) &&$this->recipientlinethree!='') {
      $recipientinfo .= $this->recipientlinethree."\n";
    }
    if (isset($this->recipientlinefour) &&$this->recipientlinefour!='') {
      $recipientinfo .= $this->recipientlinefour."\n";
    }
    if (isset($this->recipientlinefive) &&$this->recipientlinefive!='') {
      $recipientinfo .= $this->recipientlinefive."\n";
    }
    $masterclass->allclasses['paperpdf']->SetXY(117,$this->pagey);
    $masterclass->allclasses['paperpdf']->SetFont('Arial','',8);
    $masterclass->allclasses['paperpdf']->MultiCell(78,3.4,$recipientinfo,1,'L',1);
  }
  function document_totals() {
    global $masterclass;

    $arrlines = array(
      array(ucwords($this->documenttype),175,13,192,10),
      array($this->documentdate,115,17,175,13.5),
      array($this->documentid,175,17,195,13.5,1,'R'),
      array($this->subtotal,161,252.5,195,248,1,'R'),
      array($this->totaltax,161,257.5,195,253,1,'R'),
      array($this->total,161,263,195,257.5,1,'R'),
      array($this->net,59,53,99,50,1,'C'),
      array($this->documentduedate,104,53,149,50,1,'C')
      //array($this->paperwork_billingpaid,134,30.5,196,23),
      //array($this->paperwork_billingaddedby,134,30.5,196,23),
      //array($this->paperwork_billingupdated,134,30.5,196,23),
      //array($this->paperwork_billingdatestamp,134,30.5,196,23)
    );
    $masterclass->allclasses['paperpdf']->fill_page($arrlines);
  }

}


?>
